<?php


namespace app\home\controller;


use app\home\model\Nrs2002 as Nrs2002Model;
use think\Controller;
use think\Request;

class Nrs2002 extends Controller
{
    protected $nrs2002Model;

    public function __construct(
        Nrs2002Model $nrs2002Model
    )
    {
        parent::__construct();
        $this->nrs2002Model = $nrs2002Model;
    }

    public function show(Request $request)
    {
        return $this->fetch();
    }

    public function lists()
    {
        return $this->fetch();
    }

    public function ajax_lists(Request $request)
    {
        $page = $request->param('page', 1);
        $limit = $request->param('limit', 10);
        $where = [];
        $data = $this->nrs2002Model->where($where)->page($page, $limit)->select();
        $count = $this->nrs2002Model->where($where)->count();
        return reorganizeTableData(0, '', $count, $data);
    }

    public function info(Request $request)
    {
        $pid = $request->param('pid', '');
        $res = $this->nrs2002Model->where('pid', $pid)->find();
        return jsonRes($res);
    }

    public function update(Request $request)
    {
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $jiwangtizhong = $request->param('jiwangtizhong', '');
        $tjxjbfb = $request->param('tjxjbfb', '');
        $pingfen1 = $request->param('pingfen1', '');
        $pingfen2 = $request->param('pingfen2', '');
        $pingfen3 = $request->param('pingfen3', '');
        $jibingpingfen = $request->param('jibingpingfen', '');
        $bmi = $request->param('bmi', '');
        $tizhongxiajiang = $request->param('tizhongxiajiang', '');
        $sheshixiajiang = $request->param('sheshixiajiang', '');
        $is_tizhongxiajiang = $request->param('is_tizhongxiajiang', '');
        $is_sheshixiajiang = $request->param('is_sheshixiajiang', '');
        $yingyangfen = $request->param('yingyangfen', '');
        $age = $request->param('age', '');
        $pingfen = $request->param('pingfen', '');
        $data = [
            'pid' => $pid,
            'jiwangtizhong' => $jiwangtizhong,
            'tjxjbfb' => $tjxjbfb,
            'pingfen1' => $pingfen1,
            'pingfen2' => $pingfen2,
            'pingfen3' => $pingfen3,
            'jibingpingfen' => $jibingpingfen,
            'bmi' => $bmi,
            'tizhongxiajiang' => $tizhongxiajiang,
            'sheshixiajiang' => $sheshixiajiang,
            'is_tizhongxiajiang' => $is_tizhongxiajiang,
            'is_sheshixiajiang' => $is_sheshixiajiang,
            'yingyangfen' => $yingyangfen,
            'age' => $age,
            'pingfen' => $pingfen
        ];

        //判断数据是否存在
        $info = $this->nrs2002Model->where('pid', $pid)->with(['huanzhe' => function ($query) {
            $query->field('id,hospital_id,keshi,name,gender,birthday,created');
        }])->findOrEmpty()->toArray();
        if(empty($info)){
            $res = $this->nrs2002Model->save($data);
        }else{
            $res = $this->nrs2002Model->save($data,['pid' => $info['pid']]);
        }

        if ($res) {
            $resBack = jsonRes($res);
        } else {
            $resBack = jsonRes($res, '更新失败', 99);
        }
        return $resBack;
    }
}